package top.buluoluo.ordersservice.dto.respone;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import top.buluoluo.ordersservice.entity.OrderItems;
import top.buluoluo.ordersservice.entity.Orders;
import top.buluoluo.ordersservice.enums.OrderDeliveryMethod;
import top.buluoluo.ordersservice.enums.OrderPaymentMethod;
import top.buluoluo.ordersservice.enums.OrderStatus;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 订单数据传输对象 (DTO)
 */
@Data
public class OrderDTO {

    // 订单ID
    private String id;

    // 订单号（唯一业务标识）
    private String orderNo;

    // 用户ID
    private Long userId;

    // 订单状态（使用枚举名称）
    private String status;

    // 商品名称
    private String productName;

    // spuId
    private Long spuId;

    // skuId
    private Long skuId;

    private String imageUrl;
    // sku_specs
    private Map<String, String> skuSpecs;

    //商品单价
    private BigDecimal unitPrice;

    //商品数量
    private Long quantity;

    // 订单状态描述（中文）
    private String statusDescription;

    // 订单原始金额
    private BigDecimal originalAmount;

    // 折扣率
    private BigDecimal discountRate;

    // 订单最终金额
    private BigDecimal finalAmount;

    // 支付方式（使用枚举名称）
    private String paymentMethod;

    // 支付方式描述（中文）
    private String paymentMethodDescription;

    // 支付时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date paymentTime;

    // 配送方式（使用枚举名称）
    private String deliveryMethod;

    // 配送方式描述（中文）
    private String deliveryMethodDescription;

    // 配送地址（JSON结构）
    private Map<String, Object> deliveryAddress;

    // 发票信息（JSON结构）
    private Map<String, Object> invoiceInfo;

    // 订单备注
    private String remark;

    // 创建时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createdAt;

    // 更新时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updatedAt;

    /**
     * 从实体对象创建DTO
     */
    public static OrderDTO fromEntity(Orders order, OrderItems orderItems) {
        OrderDTO dto = new OrderDTO();
        dto.setId(String.valueOf(order.getId()));
        dto.setOrderNo(order.getOrderNo());
        dto.setUserId(order.getUserId());

        // 处理枚举字段
        if (order.getStatus() != null) {
            dto.setStatus(order.getStatus().name());
            dto.setStatusDescription(order.getStatus().getDescription());
        }

        dto.setOriginalAmount(order.getOriginalAmount());
        dto.setDiscountRate(order.getDiscountRate());
        dto.setFinalAmount(order.getFinalAmount());

        if (order.getPaymentMethod() != null) {
            dto.setPaymentMethod(order.getPaymentMethod().name());
            dto.setPaymentMethodDescription(order.getPaymentMethod().getDescription());
        }

        dto.setPaymentTime(order.getPaymentTime());

        if (order.getDeliveryMethod() != null) {
            dto.setDeliveryMethod(order.getDeliveryMethod().name());
            dto.setDeliveryMethodDescription(order.getDeliveryMethod().getDescription());
        }
        dto.setDeliveryAddress(order.getDeliveryAddress());
        dto.setInvoiceInfo(order.getInvoiceInfo());
        dto.setRemark(order.getRemark());
        dto.setCreatedAt(order.getCreatedAt());
        dto.setUpdatedAt(order.getUpdatedAt());
        dto.setProductName(orderItems.getProductName());
        dto.setSpuId(orderItems.getProductId());
        dto.setSkuId(orderItems.getSkuId());
        dto.setSkuSpecs(orderItems.getSkuSpecs());
        dto.setUnitPrice(orderItems.getUnitPrice());
        dto.setQuantity(orderItems.getQuantity());
        dto.setImageUrl(orderItems.getImageUrl());
        return dto;
    }

    /**
     * 转换为实体对象
     */
    public Orders toEntity() {
        Orders order = new Orders();
        order.setId(Long.valueOf(this.getId()));
        order.setOrderNo(this.getOrderNo());
        order.setUserId(this.getUserId());

        // 处理枚举字段
        if (this.getStatus() != null) {
            order.setStatus(OrderStatus.valueOf(this.getStatus()));
        }

        order.setOriginalAmount(this.getOriginalAmount());
        order.setDiscountRate(this.getDiscountRate());
        order.setFinalAmount(this.getFinalAmount());

        if (this.getPaymentMethod() != null) {
            order.setPaymentMethod(OrderPaymentMethod.valueOf(this.getPaymentMethod()));
        }

        order.setPaymentTime(this.getPaymentTime());

        if (this.getDeliveryMethod() != null) {
            order.setDeliveryMethod(OrderDeliveryMethod.valueOf(this.getDeliveryMethod()));
        }

        order.setDeliveryAddress(this.getDeliveryAddress());
        order.setInvoiceInfo(this.getInvoiceInfo());
        order.setRemark(this.getRemark());
        order.setCreatedAt(this.getCreatedAt());
        order.setUpdatedAt(this.getUpdatedAt());

        return order;
    }
}